package com.yy.controller;
import com.yy.common.annotation.Log;
import com.yy.common.constant.PmsConstants;
import com.yy.common.core.controller.BaseController;
import com.yy.common.core.domain.AjaxResult;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.enums.BusinessType;
import com.yy.config.AlipayProperties;
import com.yy.config.PayService;
import com.yy.domain.PayTradeOrder;
import com.yy.domain.PayTradeOrderItem;
import com.yy.dto.PayTradeOrderDTO;
import com.yy.service.IPayTradeOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**

 * 支付订单Controller

 * 

 * @author yy

 * @date 2023-10-31

 */

@RestController

@RequestMapping("/pay/order")

@Api(tags = "订单管理相关API")

public class PayTradeOrderController extends BaseController

{

    @Autowired
    private IPayTradeOrderService payTradeOrderService;

    @Autowired
    private AlipayProperties properties;
    @Autowired
    private PayService payService;

    

    /**

     * 收银台--保存订单

     */

    @PreAuthorize("@ss.hasPermi('pay:order:add')")
    @Log(title = "收银台-保存订单", businessType = BusinessType.INSERT)
    @PostMapping("addPayTradeOrder")
    @ApiOperation("收银台--保存订单")
    public AjaxResult addPayTradeOrder(@RequestBody @Validated PayTradeOrderDTO payTradeOrderDTO)

    {

        return toAjax(payTradeOrderService.addPayTradeOrder(payTradeOrderDTO));

    }

    

    

    /**

     * 查询支付订单列表

     */

    @PreAuthorize("@ss.hasPermi('pay:order:list')")

    @GetMapping("/listTradeOrder")

    @ApiOperation("查询支付订单列表")

    public TableDataInfo list(PayTradeOrder payTradeOrder)

    {

        startPage();

        List<PayTradeOrder> list = payTradeOrderService.selectPayTradeOrderList(payTradeOrder);

        return getDataTable(list);

    }

    

    /**

     * 根据主订单ID查询订单详情

     */

    @GetMapping("/queryTradeOrderItemByTradeId/{id}")

    @ApiOperation("根据主订单ID查询订单详情")

    public AjaxResult queryTradeOrderItemByTradeId(@PathVariable String id){

        List<PayTradeOrderItem> payTradeOrderItems=payTradeOrderService.queryTradeOrderItemByTradeId(id);

        return AjaxResult.success(payTradeOrderItems);

    }


    /**
     * 创建一个支付宝的支付订单
     */
    @GetMapping("/createPayOrder/{outTradeNo}")
    @ApiOperation("创建一个支付宝的支付订单")
    public AjaxResult createPayOrder(@PathVariable String outTradeNo){
        //根据订单编号查询订单数据
        PayTradeOrder payTradeOrder=this.payTradeOrderService.selectPayTradeOrderById(outTradeNo);
        if (ObjectUtils.isEmpty(payTradeOrder)) {
            return AjaxResult.error("【"+outTradeNo+"】订单不存在");
        }
        if(payTradeOrder.getState().equals(PmsConstants.PAY_ORDER_STATE_0)||payTradeOrder.getState().equals(PmsConstants.PAY_ORDER_STATE_1)){
            String subject=payTradeOrder.getSubject();
            String totalAmount=payTradeOrder.getTotalAmount().toString();
            String undiscountableAmount="0";
            String body=payTradeOrder.getBody();
            String notifyUrl= properties.getNotifyUrl()+"/"+outTradeNo;
            //拿二维码地址
            Map<String, Object> map = payService.pay(outTradeNo, subject, totalAmount, undiscountableAmount, body, notifyUrl);
            if(!map.get("code").equals(200)){
                return AjaxResult.error("唤起支付失败 "+map.get("msg"));
            }
            //得到二维码的地址
            String qrCode=map.get("qrCode").toString();
            //给前端
            return AjaxResult.success("操作成功",qrCode);
        }
        return AjaxResult.error("【"+outTradeNo+"】的订单不是【生成】或【支付中】状态，不能唤起支付");
    }

    /**
     * 根据支付订单ID检查订单支付状态
     */
    @GetMapping("/checkOrderStateTradeId/{tradeId}")
    public AjaxResult checkOrderStateTradeId(@PathVariable String tradeId){
        PayTradeOrder payTradeOrder = this.payTradeOrderService.selectPayTradeOrderById(tradeId);
        boolean flag=false;//订单状态不是支付成功
        if (payTradeOrder.getState().equals(PmsConstants.PAY_ORDER_STATE_2)) {
            flag=true;
        }
        return AjaxResult.success(flag);
    }


}


























